﻿namespace SimpleCalculator
{
    using System;

    using CalculatorLibrary;

    /// <summary>
    ///     Class Program
    /// </summary>
    public class Program
    {
        #region Public Methods and Operators

        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        public static void Main()
        {
            var calculator = new Calculator();
            Console.WriteLine(calculator.GetUsageHelp());
            Console.WriteLine("Specify expression, you want to calculate (or one of usage keys for help):");
            bool continueExecuting;
            do
            {
                var inputString = Console.ReadLine();
                var result = calculator.Calculate(inputString);
                continueExecuting = result.ReturnState == ReturnState.Continue;
                if (result.IsError)
                {
                    Console.WriteLine("Error: {0}", result.StringResult);
                }
                else
                {
                    if (result.Result.HasValue)
                    {
                        Console.WriteLine("Result: {0}", result.Result.Value.ToString("G"));
                    }
                    else
                    {
                        Console.WriteLine(
                            !string.IsNullOrEmpty(result.StringResult) ? result.StringResult : "No results to display!");
                    }
                }

                if (continueExecuting)
                {
                    Console.WriteLine("Specify new expression:");
                }
            }
            while (continueExecuting);

            Console.WriteLine("Press <Enter> to exit...");
            Console.ReadLine();
        }

        #endregion
    }
}